import mongoose from 'mongoose';

export { default as WxPayTransactionsJsapi } from './models/wxpay-transactions-jsapi';
export { default as WxPayNotify } from './models/wxpay-notify';
export { default as WxPayRefundNotify } from './models/wxpay-refund-notify';
export { default as WxPayRefundResult } from './models/wxpay-refund-result';

// mongoose.connect('mongodb://user:pass@localhost:port/database');

let conn;

export async function initMongoose(uri) {
  // set database
  mongoose.set('debug', process.env.NODE_ENV === 'development' ? true : process.env.MONGOOSE_LOG);
  mongoose.set('debug', true);

  /* Global plugin */
  // mongoose.plugin(updateIfCurrentPlugin);
  // const default_uri = process.env.NODE_ENV === 'production' ? 'mongodb://root:mongodb@localhost:27017/custom-checkin' : 'mongodb://localhost:27017/custom-checkin';
  const default_uri = process.env.NODE_ENV === 'production' ? 'mongodb://localhost:27017/custom-checkin' : 'mongodb://localhost:27017/custom-checkin';
  console.log(default_uri);
  return mongoose.connect(uri || process.env.mongoUri || default_uri);
}

export function getConnection() {
  return conn;
}

initMongoose()
  .then((res) => {
    conn = res;
  })
  .catch((e) => {
    console.log('########### MongoDB connect error ###########');
    console.error(e);
  });
